<?php
class Image_model extends CI_Model
{
    public function __construct()
    {
        $this->load->model("widget_model");
        $this->load->database();
    }

    public function get_images()
    {
        $widgets = $this->widget_model->get_widget_bytype('image');
        $result = array();
        if($widgets)
        {
            $widget_names = array();
            foreach($widgets as $key=>$value)
            {
                $widget_names[] = $value["name"];
            }
            $this->db->select('*');
            $this->db->from('t_image');
            $this->db->where_in('widget', $widget_names);
            $query = $this->db->get();
            $result =  $query->result_array();
        }
        return $result;
    }


    public function set_image($upload_data)
    {
        //echo $upload_data['file_name'];exit;
        $data = array(
            'imagedesc' => $this->input->post('imagedesc'),
            'imagevalue' => $upload_data['file_name'],
            'widget' => $this->input->post('widget')
        );

        return $this->db->insert('t_image',$data);
    }

    public function get_image_bywidget($widget)
    {
        $query = $this->db->get_where('t_image', array('widget' => $widget));
        return $query->result_array();
    }

    public function get_image_byid($id)
    {
        $query = $this->db->get_where('t_image', array('id' => $id));
        return $query->result_array();
    }

    public function delete_image($id)
    {
        $this->db->delete('t_image', array('id'=>$id));
    }

    public function delete_image_bywidget($widget)
    {
        $this->db->delete('t_image', array('widget'=>$widget));
    }
}
?>
